package com.ypd.shop.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ypd.common.utils.SecurityUtils;
import com.ypd.shop.domain.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ypd.shop.mapper.PropertyFavoritesMapper;
import com.ypd.shop.domain.PropertyFavorites;
import com.ypd.shop.service.IPropertyFavoritesService;

/**
 * 我的收藏Service业务层处理
 * 
 * @author lin
 * @date 2024-12-06
 */
@Service
public class PropertyFavoritesServiceImpl implements IPropertyFavoritesService 
{
    @Autowired
    private PropertyFavoritesMapper propertyFavoritesMapper;
    @Autowired
    private PropertiesServiceImpl propertiesService;

    /**
     * 查询我的收藏
     * 
     * @param favoriteId 我的收藏主键
     * @return 我的收藏
     */
    @Override
    public PropertyFavorites selectPropertyFavoritesByFavoriteId(Long favoriteId)
    {
        return propertyFavoritesMapper.selectPropertyFavoritesByFavoriteId(favoriteId);
    }

    /**
     * 查询我的收藏列表
     * 
     * @param
     * @return 我的收藏
     */
    @Override
    public List<Properties> selectPropertyFavoritesList(Properties properties)
    {
        //根据userId查找收藏列表,获取propertiesId集合
        PropertyFavorites propertyFavorites = new PropertyFavorites();
        propertyFavorites.setUserId(SecurityUtils.getUserId());
        List<PropertyFavorites> favoritesList = propertyFavoritesMapper.selectPropertyFavoritesList(propertyFavorites);
        //我想要收集favoritesList集合中的propertiesId放进同一个list中(如果有)
        if (favoritesList != null && favoritesList.size()>0){
            List<Long> ids = favoritesList.stream().map(PropertyFavorites::getPropertyId).collect(Collectors.toList());
            //获取到该用户收到的商品id集合
            properties.getParams().put("ids",ids);
            List<Properties> properties1 = propertiesService.selectPropertiesList(properties);
            return properties1;
        }else{
            return new ArrayList<Properties>();
        }
    }

    /**
     * 新增我的收藏
     * 
     * @param propertyFavorites 我的收藏
     * @return 结果
     */
    @Override
    public Long insertPropertyFavorites(PropertyFavorites propertyFavorites)
    {
        propertyFavorites.setUserId(SecurityUtils.getUserId());
        return propertyFavoritesMapper.insertPropertyFavorites(propertyFavorites);
    }

    /**
     * 修改我的收藏
     * 
     * @param propertyFavorites 我的收藏
     * @return 结果
     */
    @Override
    public Integer updatePropertyFavorites(PropertyFavorites propertyFavorites)
    {
        return propertyFavoritesMapper.updatePropertyFavorites(propertyFavorites);
    }

    /**
     * 批量删除我的收藏
     * 
     * @param favoriteIds 需要删除的我的收藏主键
     * @return 结果
     */
    @Override
    public int deletePropertyFavoritesByFavoriteIds(Long[] favoriteIds)
    {
        return propertyFavoritesMapper.deletePropertyFavoritesByFavoriteIds(favoriteIds);
    }

    /**
     * 删除我的收藏信息
     * 
     * @param favoriteId 我的收藏主键
     * @return 结果
     */
    @Override
    public int deletePropertyFavoritesByFavoriteId(Long favoriteId)
    {
        return propertyFavoritesMapper.deletePropertyFavoritesByFavoriteId(favoriteId);
    }

    /**
     * 根据店铺id查找是否已被我收藏
     * @param propertyId
     * @return
     */
    public Integer queryByPropertyId(Long propertyId) {

        return propertyFavoritesMapper.queryPropertyId(propertyId);

    }
}
